package com.zyguo.thread;

import java.util.concurrent.locks.ReentrantLock;

public class UserLock {
	private int money;
	private int id;
	private ReentrantLock lock = new ReentrantLock();
	
	public UserLock( int id ){
		this.setId(id);
	} 
	public int getMoney() {
		return money;
	}
	
	public int addMoney( int incrNum ){
		try {
			lock.lock();
			System.out.println("addMoney, lock=" + Thread.currentThread() + ",money=" + this.money );
			this.money = this.money + incrNum; 
			Thread.sleep( 3000 );
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally{
			lock.unlock();
			System.out.println("addMoney, unlock=" + Thread.currentThread() + ",money=" + this.money );
		}
		return this.money;
	}
	
	public int reducMoney( int reducNum ){
		this.money = this.money - reducNum;
		return this.money;
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
}
